<?php

namespace app\backend\controller;

use app\backend\service\CategoryService;

class Category extends Backend
{
    /**
     * 栏目列表
     * @param CategoryService $service
     * @return string|\think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index(CategoryService $service)
    {
        if ($this->request->isAjax()) {
            return $this->success($service->lists($this->request->param()));
        }
        return $this->fetch('', $service->catLabel());
    }

    /**
     * 添加栏目
     * @param CategoryService $service
     * @return string|\think\response\Json
     * @throws \app\common\RootException
     */
    public function add(CategoryService $service)
    {
        if ($this->request->isAjax()) {
            $addResult = $service->add($this->request->param());
            return $this->success($addResult);
        }
        return $this->fetch('', $service->catLabel());
    }

    /**
     * 修改文章栏目
     * @param Source
     * @param CategoryService $service
     * @return string|\think\response\Json
     * @throws \app\common\RootException
     */
    public function edit(CategoryService $service)
    {
        $params = $this->request->param();
        // 编辑操作
        if ($this->request->isAjax()) {
            $renewResult = $service->edit($params);
            return $this->success($renewResult);
        }
        // 模板渲染
        $fetchData = $service->catLabel();
        $fetchData['info'] = $service->detail($params['id']);
        return $this->fetch('', $fetchData);
    }

    /**
     * 详情
     * @param CategoryService $service
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(CategoryService $service)
    {
        // 模板渲染
        $fetchData = $service->catLabel();
        $fetchData['info'] = $service->detail($this->request->param('id'));
        return $this->fetch('', $fetchData);
    }

    /**
     * 修改排序
     * @param CategoryService $service
     * @return \think\response\Json
     */
    public function editRank(CategoryService $service)
    {
        return $this->success($service->editRank($this->request->param()));
    }
}